package com.tool.builder.BuildClass;

import com.tool.bean.Constants;
import com.tool.bean.FieldInfo;
import com.tool.bean.TableInfo;
import com.tool.builder.BuildHelper.BuildComment;
import com.tool.utils.DateUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * 创建po包
 */
public class BuildPo {
    private static final Logger logger = LoggerFactory.getLogger(BuildPo.class);

    public static void execute(TableInfo tableInfo) {
        File folder = new File(Constants.PATH_PO);
        if(!folder.exists()) {   // 路径不存在则创建
            folder.mkdirs();
        }

        File poFile = new File(folder, tableInfo.getBeanName() + ".java");

        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;
        try {
            out = new FileOutputStream(poFile);
            outw = new OutputStreamWriter(out, "utf8");
            bw = new BufferedWriter(outw);

            bw.write("package " + Constants.PACKAGE_PO + ";");
            bw.newLine();
            bw.newLine();

            bw.write("import java.io.Serializable;");
            bw.newLine();
            bw.write("import lombok.Data;");
            bw.newLine();

            bw.write("import com.baomidou.mybatisplus.annotation.IdType;");
            bw.newLine();
            bw.write("import com.baomidou.mybatisplus.annotation.TableId;");
            bw.newLine();

            if(tableInfo.getHaveDate() || tableInfo.getHaveDateTime()) {
                bw.write("import java.util.Date;");
                bw.newLine();
                bw.write(Constants.BEAN_DATE_FORMAT_CLASS);
                bw.newLine();
                bw.write(Constants.BEAN_DATE_UNFORMAT_CLASS);
                bw.newLine();
            }

            // 忽略属性
            Boolean haveIgnoreBean = false;
            for(FieldInfo field : tableInfo.getFieldList()) {
                if(ArrayUtils.contains(Constants.IGNORE_BEAN_TOJSON_FILED.split(","), field.getPropertyName())) {
                    haveIgnoreBean = true;
                    break;
                }
            }
            if(haveIgnoreBean) {
                bw.write(Constants.IGNORE_BEAN_TOJSON_CLASS );
                bw.newLine();
            }

            if(tableInfo.getHaveBigDecimal()) {
                bw.write("import java.math.BigDecimal;");
                bw.newLine();
            }
            bw.newLine();

            // 构建类注释
            BuildComment.createClassComment(bw, tableInfo.getComment());
            bw.newLine();

            bw.write("@Data");
            bw.newLine();
            bw.write("public class " + tableInfo.getBeanName() + " implements Serializable {");
            bw.newLine();

            for(FieldInfo field : tableInfo.getFieldList()) {
                BuildComment.createFieldComment(bw, field.getComment());

                if(ArrayUtils.contains(Constants.SQL_DATE_TIME_TYPES, field.getSqlType())) {
                    bw.write("\t" +String.format(Constants.BEAN_DATE_FORMAT_EXPRESSION, DateUtils.YYYY_MM_DD_HH_MM_SS));
                    bw.newLine();
                    bw.write("\t" +String.format(Constants.BEAN_DATE_UNFORMAT_EXPRESSION, DateUtils.YYYY_MM_DD_HH_MM_SS));
                    bw.newLine();
                }
                if(ArrayUtils.contains(Constants.SQL_DATE_TYPES, field.getSqlType())) {
                    bw.write("\t" +String.format(Constants.BEAN_DATE_FORMAT_EXPRESSION, DateUtils.YYYY_MM_DD));
                    bw.newLine();
                    bw.write("\t" +String.format(Constants.BEAN_DATE_UNFORMAT_EXPRESSION, DateUtils.YYYY_MM_DD));
                    bw.newLine();
                }

                if(ArrayUtils.contains(Constants.IGNORE_BEAN_TOJSON_FILED.split(","), field.getPropertyName())) {
                    bw.write("\t" +String.format(Constants.IGNORE_BEAN_TOJSON_EXPRESSION, DateUtils.YYYY_MM_DD));
                    bw.newLine();
                }

                if(field.getPropertyName().equalsIgnoreCase(field.getKey())) {
                    if(field.getAutoIncrement()) {
                        bw.write("\t@TableId(type = IdType.AUTO)");
                        bw.newLine();
                    }
                    else {
                        bw.write("\t@TableId");
                        bw.newLine();
                    }
                }

                bw.write("\tprivate " + field.getJavaType() + " " + field.getPropertyName() + ";");
                bw.newLine();
                bw.newLine();
            }

            bw.write("}");
            bw.flush();
        }   catch (Exception e) {
            logger.error("创建po失败", e);
        }  finally {
            if(outw !=null) {
                try {
                    outw.close();
                }   catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
